﻿using System.Runtime.Serialization;

namespace Order
{
    [DataContract]
    public class OrderDetail : System.ICloneable
    {
        [DataMember]
        public int ID { get; init; }
        [DataMember]
        public Commodity Commodity { get; set; }
        [DataMember]
        public int Amount { get; set; }
        public OrderDetail()
        {
            Commodity = new();
            ID = base.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            if (obj is not OrderDetail d) return false;
            if (!Commodity?.Equals(d.Commodity) ?? Commodity != d.Commodity) return false;
            if (Amount != d.Amount) return false;
            return true;
        }

        public override int GetHashCode()
        {
            return Commodity?.GetHashCode() ^ Amount ?? 0;
        }

        public override string ToString()
        {
            return Commodity + "\tAmount: " + Amount;
        }

        public object Clone()
        {
            return new OrderDetail()
            {
                ID = ID,
                Amount = Amount,
                Commodity = Commodity.Clone() as Commodity
            };
        }
    }
}
